package com.game.commons.configuration.transformers;

import java.lang.reflect.Field;

import com.game.commons.configuration.PropertyTransformer;
import com.game.commons.configuration.TransformationException;


/**
 * 字符串转换成char类型
 * @author 杜祥
 * @create 2013-6-29
 */
public class CharTransformer implements PropertyTransformer<Character>
{
	public static final CharTransformer	SHARED_INSTANCE	= new CharTransformer();

	/**
	 * Transforms string to character
	 * 
	 * @param value
	 *            value that will be transformed
	 * @param field
	 *            value will be assigned to this field
	 * @return Character object that represents transformed string
	 * @throws TransformationException
	 *             if something went wrong
	 */
	@Override
	public Character transform(String value, Field field) throws TransformationException
	{
		try
		{
			char[] chars = value.toCharArray();
			if(chars.length > 1)
			{
				throw new TransformationException("To many characters in the value");
			}

			return chars[0];
		}
		catch(Exception e)
		{
			throw new TransformationException(e);
		}
	}
}
